import model
from training import train2
from alignment import split_smart
import char_inputs



class Converter(object):
    def __init__(self, data_file, greek_file='greekwords.txt', grkl_file='wordlist2.txt' ):
        try:
            self.model= model.read_model(data_file)
            self.parts = self.model.observations
            self.parts.sort(key=len, reverse=True)

        except:
            print "Model not found, training..."
            self.model = train2(greek_file,grkl_file)
            model.dump_model(self.model, data_file)  
            self.parts = self.model.observations
            self.parts.sort(key=len, reverse=True)
            
    def convert(self,word, join=True):
        sequence = split_smart(word.lower(),self.parts, char_inputs.greeklish_split)
        
        if(sequence):
            result = self.model.viterbi(sequence)
            if join:return ''.join(result)
            else: return result
        else:
#            print 'ERROR: could not split word',word.lower()
            
            return None
    
    def probability(self,word):
        sequence = split_smart(word.lower(),self.parts, char_inputs.greeklish_split)
        if(sequence):
            result = self.model.forward(sequence)
            return result
        return None
    
    def sequence(self,word):
        return split_smart(word.lower(),self.parts, char_inputs.greeklish_split)